'use strict'
const CryptoJS = require('crypto-js')

module.exports = (app) => {
  const { STRING, INTEGER, DATE } = app.Sequelize
  // 配置（重要：一定要配置详细，一定要！！！）
  const User = app.model.define('user', {
    id: {
      type: INTEGER(20).UNSIGNED,
      primaryKey: true,
      autoIncrement: true
    },
    username: {
      type: STRING(30),
      comment: '用户名称'
    },
    nickname: {
      type: STRING(30),
      comment: '昵称'
    },
    email: {
      type: STRING(160),
      comment: '用户邮箱'
    },
    password: {
      type: STRING,
      comment: '密码',
      set(value) {
        // 加密
        const secret = app.config.crypto.secret
        const ciphertext = CryptoJS.HmacSHA256(value, secret).toString()
        this.setDataValue('password', ciphertext)
      }
    },
    avatar: {
      type: STRING(500),
      comment: '头像'
    },
    status: {
      type: INTEGER(1),
      defaultValue: 1,
      comment: '状态-1:正常,2:禁用'
    },
    type: {
      type: INTEGER(1),
      defaultValue: 1,
      comment: '类型-1:用户,2:游客'
    },
    ip: {
      type: STRING(100),
      comment: 'ip地址'
    },
    created_time: {
      type: DATE,
      get() {
        return app.formatTime(this.getDataValue('created_time'))
      }
    }
  })

  return User
}
